package bmatch.tech.file.resource;

import bmatch.tech.file.model.Response;
import bmatch.tech.file.service.MyFileService;
import bmatch.tech.file.service.impl.MyFileServiceImpl;
import org.glassfish.jersey.media.multipart.FormDataParam;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.io.InputStream;

/**
 * MyFileResource
 *
 * @author: Awake
 * @date: 2018-11-29 17:16
 **/

@Path("file")
public class MyFileResource {

    private static MyFileService service;

    static {
        service = new MyFileServiceImpl();
    }

    @POST
    @Path("upload")
    @Consumes({MediaType.MULTIPART_FORM_DATA})
    @Produces(MediaType.APPLICATION_JSON)
    public Response upload(
            @FormDataParam("file") InputStream inputStream,
            @FormDataParam("userId") String userId,
            @FormDataParam("path") String path,
            @FormDataParam("name") String name) {

        return service.upload(inputStream, userId, path, name);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("delete")
    public Response delete(@QueryParam("userId") String userId,
                           @QueryParam("path") String path,
                           @QueryParam("name") String name) {
        return service.delete(userId, path, name);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("rename")
    public Response rename(@QueryParam("userId") String userId,
                           @QueryParam("path") String path,
                           @QueryParam("originalName") String originaName,
                           @QueryParam("targetName") String targetName) {
        return service.rename(userId, path, originaName, targetName);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("shift")
    public Response shift(@QueryParam("userId") String userId,
                          @QueryParam("name") String name,
                          @QueryParam("originalPath") String originalPath,
                          @QueryParam("targetPath") String targetPath) {
        return service.shift(userId, name, originalPath, targetPath);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("download")
    public javax.ws.rs.core.Response downloadFile(
            @QueryParam("userId") String userId,
            @QueryParam("path") String path,
            @QueryParam("name") String name) {
        return service.downloadFile(userId, path, name);
    }
}
